package org.example.manage;

import org.example.model.MilkTea;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class MilkTeaManage extends ObjectManage<MilkTea>{
    private static List<Integer> kindI;
    public MilkTeaManage() {
        type = 1;
        setFileName("data/milk_file/milk.json");
    }
    @Override
    public boolean compare(MilkTea item, MilkTea uItem) {
        boolean b = item.milkName.equals(uItem.milkName)&&
                item.price == uItem.price&&
                item.inventory == uItem.inventory&&
                item.imagePath.equals(uItem.imagePath)&&
                item.kind.equals(uItem.kind);
        return b;
    }
    public static List<String> getKinds(List<MilkTea> milkTeas) {
        List<String> kinds = new ArrayList<>();
        kindI = new ArrayList<>();
        if (milkTeas.isEmpty()) {
            return kinds;
        }
        kinds.add(milkTeas.getFirst().kind);
        kindI.add(0);
        for (int i = 1; i < milkTeas.size(); i++) {
            if (!milkTeas.get(i).kind.equals(milkTeas.get(i - 1).kind)) {
                kinds.add(milkTeas.get(i).kind);
                kindI.add(i);
            }
        }
        return kinds;
    }
    public static List<Integer> getKindI() {
        return kindI;
    }
    public static List<MilkTea> listClassify(List<MilkTea> milkTeas) {
        Stack<MilkTea> stackOne = new Stack<>(),
                stackTwo = new Stack<>();
        if (milkTeas.isEmpty()) {
            return milkTeas;
        }
        stackOne.push(milkTeas.getFirst());
        List<MilkTea> list = new ArrayList<>();
        for (int i = 1; i < milkTeas.size(); i++) {
            while (!stackOne.isEmpty()){
                if (stackOne.peek().kind.equals(milkTeas.get(i).kind)) {
                    stackOne.add(milkTeas.get(i));
                    break;
                }
                else stackTwo.push(stackOne.pop());
            }
            if (stackOne.isEmpty()) {
                stackOne.push(milkTeas.get(i));
            }
            while (!stackTwo.isEmpty()) {
                stackOne.push(stackTwo.pop());
            }
        }
        list = stackOne.reversed();
        return list;
    }
}
